package com.qc.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.OpenCityEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.OpenCityService;

import net.sf.json.JSONObject;


@Controller
@RequestMapping("/web")
public class OpenCityController {
	
	@Resource(name="openCityService")
	private OpenCityService openCityService;

	@RequestMapping(value = "/toCity", produces = "text/html;charset=UTF-8")
    public ModelAndView toCity(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/verify/city");
        
        return mav;
    }
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/toListOpenCity", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListOpenCity(HttpServletRequest request) {
        
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = openCityService.getListOpenCityCount();
        
        //根据条件查询当前页数据
        List<OpenCityEntity> list = openCityService.getAllOpenCity(pageNo, pageSize);
        
        //分页查询公共类
        Page<OpenCityEntity> page = new Page<OpenCityEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<OpenCityEntity> pageModel = new PageModelList<OpenCityEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
	
	/**
	 * 开通城市 添加
	 * @param request
	 * @param openCityEntity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toSaveOpen")
    public ModelAndView toSaveOpen(HttpServletRequest request,OpenCityEntity openCityEntity) throws Exception {
		
		ModelAndView mav = new ModelAndView("redirect:/web/toCity.do");
		
		openCityService.saveOpenCity(openCityEntity);
		
		return mav;
	}
	
	/**
	 * 开通城市删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toOpenCityId")
    @ResponseBody
    public int toDeleteOpenId(HttpServletRequest request, HttpServletResponse response) {
		
        long bId = Long.parseLong(request.getParameter("thisid"));
        
        int flag = openCityService.deleteOpenCityId(bId);
        return flag;
    }
}
